<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples\GroupPink;
use App\Admin\Metrics\Examples\PinkList;
use App\Admin\Renderable\GoodsTable;
use App\Admin\Renderable\StoreDetails;
use App\Admin\Repositories\StorePink;
use App\Models\GrGood;
use App\Models\StoreCombination;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Http\Controllers\AdminController;

class StorePinkController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('拼团列表')
            ->description('拼团列表')
            ->body(function (Row $row) {
                $row->column(4, new PinkList());
                $row->column(4, new GroupPink());
            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new StorePink(), function (Grid $grid) {
            $grid->column('id')->sortable();
//            $grid->column('uid');
            $grid->column('avatar')->image('', 100, 100);
            $grid->column('nickname');


            $grid->column('cid')->display(function ($e) {
                $data = StoreCombination::where('id', $this->pid)->pluck('product_id');
                $data = GrGood::where('goods_id', $data)->pluck('goods_name');
                return ($data->toArray()[0]);
            });
            $grid->column('add_time');
            $grid->column('headcount');
            $grid->column('turnout');
            $grid->column('stop_time');
            $grid->column('status')->using([
                1 => '进行中',
                2 => '已完成',
                3 => '未完成',
            ], 'Unknown')->dot([
                1 => 'danger',
                2 => 'info',
                3 => 'using',
            ], 'warning');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '拼团状态', [
                    1 => '进行中',
                    2 => '已完成',
                    3 => '未完成',
                ]);

            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                // 注意切换为panel布局方式时需要重新调整表单字段的宽度
//                $filter->equal('id')->width(3);
                // 设置datetime类型
                $filter->between('add_time', '选择时间')->datetime()->width(4);
                $filter->equal('status', '拼团状态')->select([
                    1 => '进行中',
                    2 => '已完成',
                    3 => '未完成',
                ])->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new StorePink(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('order_id');
            $show->field('order_id_key');
            $show->field('total_num');
            $show->field('total_price');
            $show->field('cid');
            $show->field('pid');
            $show->field('people');
            $show->field('price');
            $show->field('add_time');
            $show->field('stop_time');
            $show->field('k_id');
            $show->field('is_tpl');
            $show->field('is_refund');
            $show->field('status');
            $show->field('is_virtual');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new StorePink(), function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');

                // 显示底部提交按钮
                $form->showFooter();

                // 设置字段宽度
                $form->width(9, 2);

                $form->column(6, function (Form\BlockForm $form) {
                    $form->display('id');
                    $form->text('name');
                    $form->email('email');
                    $form->image('avatar')->uniqueName()->autoUpload()->saveFullUrl()->accept('jpg,png,gif,jpeg');
                    $form->password('password');
                });

                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('username');
                    $form->email('mobile');
                    $form->textarea('description');
                });
            });
            $form->block(4, function (Form\BlockForm $form) {
                $form->title('分块2');

                $form->text('nickname');
                $form->number('age');
                $form->radio('status')->options(['1' => '默认', 2 => '冻结'])->default(1);

                $form->next(function (Form\BlockForm $form) {
                    $form->title('分块3');

                    $form->date('birthday');
                    $form->date('created_at');
                });
            });
//            $form->display('id');
//            $form->text('uid');
//            $form->text('nickname');
//            $form->text('avatar');
//            $form->text('order_id');
//            $form->text('order_id_key');
//            $form->text('total_num');
//            $form->text('total_price');
//            $form->text('cid');
//            $form->text('pid');
//            $form->text('people');
//            $form->text('price');
//            $form->text('add_time');
//            $form->text('stop_time');
//            $form->text('k_id');
//            $form->text('is_tpl');
//            $form->text('is_refund');
//            $form->text('status');
//            $form->text('is_virtual');
//
//            $form->display('created_at');
//            $form->display('updated_at');
        });
    }
}
